package com.yexs.common.vo;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yexs
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -7775863438873862469L;

    private Integer code = 0;
    private String message = "成功";
    private T result;
    private Map<String, Object> data;

    private Result(Integer code, String msg) {
        this(code, msg, null);
    }

    private Result(Integer code, String message, T result) {
        this.code = code;
        this.message = message;
        this.result = result;
    }

    public static <T> Result<T> ERROR(String message) {
        return ERROR(10000, message, null);
    }

    public static <T> Result<T> ERROR(String message, T result) {
        return ERROR(10000, message, result);
    }

    public static <T> Result<T> ERROR(Integer code, String message) {
        return ERROR(code, message, null);
    }

    public static <T> Result<T> ERROR(Integer code, String message, T result) {
        return new Result<>(code, message, result);
    }

    public static Result<Void> SUCCESS() {
        return SUCCESS(null);
    }

    public static Result<Void> SUCCESS(String message) {
        return SUCCESS(message, null);
    }

    public static <T> Result<T> SUCCESS(T result) {
        return SUCCESS(null, result);
    }

    public static <T> Result<T> SUCCESS(String message, T result) {
        return new Result<>(0, message == null ? "成功" : message, result);
    }

    public void put(String key, Object value) {
        if (data == null) {
            data = new HashMap<>();
        }
        data.put(key, value);
    }

    public Object get(String key) {
        if (data == null) {
            return null;
        }
        return data.get(key);
    }

    public Map<String, Object> data() {
        return data;
    }

    public boolean success() {
        return code == 0;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

}
